<?php
namespace Home\Model;
use Think\Model;
class OrderModel extends Model {
	public $table='zjy_order';
	public $page; //当前页
	public $perpage; //每页显示
	public $purchase_id; //ID
	public $order_id; //订单ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类
	public $member_id; //会员ID
	public $data; //传递的数组数据

	//获取列表
	public function get_purchase_order_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 '.'and buyer_id='.$this->member_id;
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$list=$Model->query("select * from ".$this->table." ".$where." order by order_id desc limit $start,$perpage");
		foreach($list as $k=>$v){
			$list[$k]['seller_name']=$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$list[$k]['buyer_name']=$Member->where('member_id='.$v['buyer_id'])->getField('member_truename');
		}
		return $list;
	}
	//获得总数
	public function get_purchase_order_total()
	{
		//搜索
		$where='where 1 '.'and seller_id='.$this->member_id;
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$query=$Model->query("select count(order_id) as total from ".$this->table." ".$where." ");
		return $query[0]['total'];
	}
	public function get_supply_order_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 '.'and seller_id='.$this->member_id;
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$list=$Model->query("select * from ".$this->table." ".$where." order by order_id desc limit $start,$perpage");
		foreach($list as $k=>$v){
			$list[$k]['seller_name']=$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$list[$k]['buyer_name']=$Member->where('member_id='.$v['buyer_id'])->getField('member_truename');
		}
		return $list;
	}
	//获得总数
	public function get_supply_order_total()
	{
		//搜索
		$where='where 1 '.'and seller_id='.$this->member_id;
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$query=$Model->query("select count(order_id) as total from ".$this->table." ".$where." ");
		return $query[0]['total'];
	}
	//获取信息
	public function get_order_info()
	{
		$order_id=intval($this->order_id);
		$Model=M("Order");
		$Member=M("Member");
		$OrderProduct=M("Order_product");
		$MemberAddress=M("Member_address");
		$Match=M("Match");
		$Purchase=M("Purchase");
		$Supply=M("Supply");
		$info=$Model->where('order_id='.$order_id)->find();
		$info['match']=$Match->where('match_id='.$info['match_id'])->find();
		$info['purchase_title']=$Purchase->where('purchase_id='.$info['match']['purchase_id'])->getField('purchase_title');
		$info['supply_title']=$Supply->where('supply_id='.$info['match']['supply_id'])->getField('supply_title');
		//print_r($info);exit;
		$info['seller_name']=$Member->where('member_id='.$info['seller_id'])->getField('member_truename');
		$info['buyer_name']=$Member->where('member_id='.$info['buyer_id'])->getField('member_truename');
		$info['product_list']=$OrderProduct->where('order_id='.$order_id)->order('rec_id asc')->select();
		$info['daddress_id']=$Model->where('order_id='.$order_id)->getField('daddress_id');
		$daddress_id=intval($info['daddress_id']);
		$info['member_address']=$MemberAddress->where('rec_id='.$daddress_id.'')->find();
		$info['member_balance']=$Member->where('member_id='.$info['buyer_id'])->getField('member_balance');
		$info['shipping_type_title']=M('Shipping_type')->where('st_id='.$info['shipping_type'])->getField('st_title');
		$info['shipping_express_name']=M('Shipping_express')->where('shipping_express_type='.$info['shipping_type'].' and shipping_express_id='.$info['shipping_express_id'])->getField('shipping_express_name');
		//print_r($info['member_address']);exit;
		//echo $info['shipping_express_name'];exit;
		return $info;
	}
	//编辑提交
	public function order_edit_post()
	{
		$Model=M("Order");
		$OrderAddress=M("Order_address");
		$condition['order_id'] = $this->post['order_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		$result2=$OrderAddress->where($condition)->save($data);
		if($result===false||$result2===false) {
			return false;
		}else{
			return true;
		}
	}
	//检测 不存在则写入订单
	public function check_add_order(){
		$Model=M("Order");
		//echo $this->match_id;
		//查询是否已经存在
		$num=$Model->where('match_id='.$this->data['match_id'])->count();
		//echo $num;exit;
		//print_r($this->data);exit;
		if($num==0){
			//写入
			$data=$this->data;
			if(!$Model->add($data)){
				return false;
			}
		}
		return true;
	}

	//获得列表 $page当前页 $perpage每页显示
	public function get_order_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=M('Order');
		$Member=M("Member");
		$Match=M("Match");
		$Purchase=M("Purchase");
		$Spec=M("Spec");
		$Depot=M("Depot");
		$Company=M("Company");
		$list=$Model->query("select * from ".$this->table." ".$where." order by order_id desc limit $start,$perpage");
		foreach($list as $k=>$v){
			//$list[$k]['seller_name']=$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$list[$k]['buyer_name']=$Company->where('member_id='.$v['buyer_id'])->getField('company_name');
			$purchase_id=$Match->where('match_id='.intval($v['match_id']))->getField('purchase_id');
			$list[$k]['purchase_info']=$Purchase->where('purchase_id='.$purchase_id)->find();
			$list[$k]['purchase_info']['spec_name']=$Spec->where('spec_id='.intval($list[$k]['purchase_info']['spec_id']))->getField('spec_name');
			$list[$k]['purchase_info']['depot_name']=$Depot->where('depot_id='.intval($list[$k]['purchase_info']['depot_id']))->getField('depot_name');
			$list[$k]['time_to_word']=$this->time_to_str(intval($v['finished_time']));
		}
		return $list;
	}
	private function time_to_str($time) {
		$time = (int) substr($time, 0, 10);
		$int = time() - $time;
		$str = '';
		if ($int <= 2){
			$str = sprintf('刚刚', $int);
		}elseif ($int < 60){
			$str = sprintf('%d秒前', $int);
		}elseif ($int < 3600){
			$str = sprintf('%d分钟前', floor($int / 60));
		}elseif ($int < 86400){
			$str = sprintf('%d小时前', floor($int / 3600));
		}elseif ($int < 2592000){
			$str = sprintf('%d天前', floor($int / 86400));
		}else{
			$str = date('Y-m-d H:i:s', $time);
		}
		return $str;
	}



}